import QtQuick 2.0
import QtCharts 2.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
//![1]
ChartView {
    id: chartView
    animationOptions: ChartView.NoAnimation
    theme: ChartView.ChartThemeDark
    property var temArray:[]
    property var humArray:[]
    //wind speed
    ValueAxis {
        id: axisY_speed
        min: -50.0
        max: 50.0
        titleText:"温度[°C]"
    }
    //wind yaw
    ValueAxis {
        id: axisY_yaw
        min: 0.0
        max: 100.0
        titleText:"湿度[%]"
    }
    ValueAxis {
        id: axisX
        min: 0
        max: 100
    }
    ScatterSeries{
        markerSize:8
        id: scatterSeries1
        axisX: axisX
        axisY: axisY_speed
        onHovered: {
            var p = chartView.mapToPosition(point,scatterSeries1)
            var v = chartView.mapToValue(p,scatterSeries1)
            showDat(v.x)
        }
    }
    LineSeries {
        id: lineSeries1
        name: "温度"
        axisX: axisX
        axisY: axisY_speed
    }
    ScatterSeries{
        id: scatterSeries2
        markerSize:8
        axisX: axisX
        axisYRight:axisY_yaw
        onHovered: {
            var p = chartView.mapToPosition(point,scatterSeries1)
            var v = chartView.mapToValue(p,scatterSeries1)
            showDat(v.x)
        }
    }
    LineSeries {
        id: lineSeries2
        name: "湿度"
        axisX: axisX
        axisYRight:axisY_yaw
    }
    function resetChart()
    {
        scatterSeries1.clear()
        scatterSeries2.clear()
        lineSeries1.clear()
        lineSeries2.clear()
    }
    function updateChart(temperature,humidity)
    {
        var  len = lineSeries1.count;
        axisX.max = len
        lineSeries1.append(len,temperature);
        scatterSeries1.append(len,temperature);
        temArray.push(temperature);

        lineSeries2.append(len,humidity);
        scatterSeries2.append(len,humidity);
        humArray.push(humidity);
        var speed_max = temArray[0] + 1;
        var speed_min = temArray[0] - 1;
        var alt_max = humArray[0] + 1;
        var alt_min = humArray[0] - 1;
        for(var i = 0;i < len;i++ )
        {
            if(speed_max < temArray[i])
                speed_max = temArray[i] + 1;
            else if(speed_min > temArray[i])
                speed_min = temArray[i] - 1;

            if(alt_max < humArray[i])
                alt_max = humArray[i] + 1;
            else if(alt_min > humArray[i])
                alt_min = humArray[i] - 1;
        }
        axisY_speed.max = speed_max;
        axisY_speed.min = speed_min;
        axisY_yaw.max = alt_max;
        axisY_yaw.min = alt_min;
    }
    MouseArea{
        id:mouseArea
        anchors.fill: parent
        property int currentX:0
        property int currentY: 0

        onWheel: {//图形缩放
            if(wheel.angleDelta.y>0){
                chartView.zoom(1.1)
                chartView.scrollRight(40)
            }
            else{
                chartView.zoom(0.9)
            }
        }
        onPressed: {//获取点击时位置
            mouseArea.cursorShape = Qt.ClosedHandCursor
            currentX = mouse.x
            currentY = mouse.y
        }

        onReleased: {
            mouseArea.cursorShape = Qt.ArrowCursorArrowCursor

        }

        onPositionChanged:{//拖拽功能实现
            var moveX = mouse.x-currentX
            var moveY = mouse.y-currentY
            currentX = mouse.x
            currentY = mouse.y
            chartView.scrollLeft(moveX)
            chartView.scrollUp(moveY)
        }
    }
}
